//
//  testUIViewController.m
//  商城
//
//  Created by runone02 on 2018/9/17.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "testUIViewController.h"
#import "TopView.h"
#import "searchView.h"
#import "SwitchGridCell.h"  //格子样式
#import "ListGridCell.h"   //列表样式
// Models
#import "DCRecommendItem.h"
#import "ChangeViewFlowLayout.h"  //布局

@interface testUIViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

/* scrollerVew */
@property (nonatomic, strong)UICollectionView *collectionView;
/* 具体商品数据 */
@property (nonatomic, strong)NSMutableArray<DCRecommendItem *> *setItem;
/** 1：列表视图，0：格子视图*/
@property (nonatomic, assign) BOOL isSwitchGrid;
/** 头部View*/
@property (nonatomic, strong) TopView *topView;
/** 搜索框*/
@property (nonatomic, strong) searchView *searchView;
/** 布局*/
@property (nonatomic,strong) ChangeViewFlowLayout *layout;

@property (nonatomic, assign) BOOL isOpen; // 判断时候点击分组
/** */
@property (nonatomic,assign) NSInteger nowIndex; // 点击之后的tag值

@end

static NSString *const SwitchGridCellID = @"SwitchGridCell";
static NSString *const ListGridCellID = @"ListGridCell";


@implementation testUIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _nowIndex = -1;
    [self loadUIView];
    [self setUpData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TopOne) name:@"TopOne" object:nil];
}

- (void)loadUIView{
    // 默认列表视图
    
    _isSwitchGrid = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor lightGrayColor];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    

    
    self.searchView = [[searchView alloc]init];
    self.searchView.backgroundColor = [UIColor whiteColor];
    //切换视图
    [self.searchView.switchViewButton addTarget:self action:@selector(switchViewButtonBarItemClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //搜索
    [self.searchView.searchButton addTarget:self action:@selector(searchButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.searchView];
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(kTopBarHeight);
       
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(30);
    }];
    
    self.topView = [[TopView alloc]init];
    self.topView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.searchView.mas_bottom).with.offset(10);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(60);
    }];
    
    self.layout = [ChangeViewFlowLayout new];
    self.layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 0);  //组头高度
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor lightGrayColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[SwitchGridCell class] forCellWithReuseIdentifier:SwitchGridCellID];//cell
    
    [_collectionView registerClass:[ListGridCell class] forCellWithReuseIdentifier:ListGridCellID];//cell
    
     [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

    [self.view addSubview:_collectionView];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topView.mas_bottom).with.offset(5);
        make.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
    }];
}


#pragma mark - 加载数据
- (void)setUpData{

    _setItem = [DCRecommendItem mj_objectArrayWithFilename:@"ClasiftyGoods.plist"];
}

- (void)TopOne{
    [self setUpData];
    [self.collectionView reloadData];
    [self.collectionView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    if (_isSwitchGrid == 1) {
        // 判断打开的是哪个分区
        if (_isOpen) { // 如果打开分组
            return _setItem.count;
        }
        return 0;
    }
    return _setItem.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (_isSwitchGrid == 1) {
        return 3;
    }else{
        return 1;
    }
}


#pragma mark - <UICollectionViewDelegate>
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_isSwitchGrid == 0) {
        ListGridCell *cell = nil;
        cell =  [collectionView dequeueReusableCellWithReuseIdentifier:SwitchGridCellID forIndexPath:indexPath];
        
        cell.youSelectItem = _setItem[indexPath.row];
        return cell;
    }
    //列表
        ListGridCell *cell = nil;
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ListGridCellID forIndexPath:indexPath] ;
       cell.youSelectItem = _setItem[indexPath.row];
        return cell;
}


#pragma mark - item宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return (_isSwitchGrid) ? CGSizeMake(ScreenW, 70) : CGSizeMake((ScreenW - 8)/2, (ScreenW - 4)/2 + 60);//列表、网格Cell
}


#pragma mark - 边间距属性默认为0
#pragma mark - X间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 4;
}

#pragma mark - Y间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return (_isSwitchGrid) ? 0 : 4;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_isSwitchGrid == 0) {
          NSLog(@"格子第%ld组,第%ld行",indexPath.section,indexPath.row);
        
    }else{
          NSLog(@"列表第%ld组,第%ld行",indexPath.section,indexPath.row);
    }
}


#pragma mark - 切换视图按钮点击
- (void)switchViewButtonBarItemClick:(UIButton *)button{
    button.selected = !button.selected;
    _isSwitchGrid = !_isSwitchGrid;
    if (_isSwitchGrid == 1) {
        self.layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 50);  //组头高度
        self.isOpen = NO;
    }else{
        self.layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 0);  //组头高度
        
    }
    
    [self.collectionView reloadData];
    [self.collectionView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
}

#pragma mark - 点击搜索
- (void)searchButtonClick{
    NSLog(@"搜索");
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.backgroundColor = [UIColor redColor];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = header.bounds;
        [button setTitle:[NSString stringWithFormat:@"%ld", indexPath.section] forState:UIControlStateNormal];
        [button addTarget:self action:sel_registerName("doOpen:") forControlEvents:UIControlEventTouchUpInside];
        button.tag = 1000 + indexPath.section;
        for (UIView *view in header.subviews) {
            [view removeFromSuperview];
        } // 防止复用分区头
        [header addSubview:button];
        return header;
    } else {
        return nil;
    }
}



- (void)doOpen:(UIButton *)sender {
    NSLog(@"%ld",sender.tag);
    if (_nowIndex == sender.tag - 1000) {
        _isOpen = !_isOpen;
    } else {
        _isOpen = NO;
        _nowIndex = sender.tag - 1000;
    }
    [_collectionView reloadData];
}

@end
